package itsm.isperp.report.analyzer;

import itsm.isperp.report.Counter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.github.abel533.echarts.series.Line;

/**
 * 
 * 排序分析器
 * 
 * @author lizx
 * @date 2015-9-6
 * @since 1.0
 * 
 */
public class SortAnalyzer {

	/**
	 * 想型数据排序
	 * 
	 * @param oriMap
	 * @return
	 */
	public static Map<String, Line> sortLine(Map<String, Line> oriMap) {
		if (oriMap == null || oriMap.isEmpty()) {
			return null;
		}
		Map<String, Line> sortedMap = new LinkedHashMap<String, Line>();
		List<Map.Entry<String, Line>> entryList = new ArrayList<Map.Entry<String, Line>>(
				oriMap.entrySet());
		Collections.sort(entryList, new MapValueLineCountComparator());
		Iterator<Map.Entry<String, Line>> iter = entryList.iterator();
		Map.Entry<String, Line> tmpEntry = null;
		while (iter.hasNext()) {
			tmpEntry = iter.next();
			sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
		}
		return sortedMap;
	}

	public static Map<String, Counter> sortMap(Map<String, Counter> oriMap,
			Comparator<Map.Entry<String, Counter>> comparator) {
		if (oriMap == null || oriMap.isEmpty()) {
			return null;
		}
		Map<String, Counter> sortedMap = new LinkedHashMap<String, Counter>();
		List<Map.Entry<String, Counter>> entryList = new ArrayList<Map.Entry<String, Counter>>(
				oriMap.entrySet());
		Collections.sort(entryList, comparator);
		Iterator<Map.Entry<String, Counter>> iter = entryList.iterator();
		Map.Entry<String, Counter> tmpEntry = null;
		while (iter.hasNext()) {
			tmpEntry = iter.next();
			sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
		}
		return sortedMap;
	}

	public static Map<String, Counter> sortMapByOrderNum(
			Map<String, Counter> oriMap) {
		return sortMap(oriMap, new MapValueCounterOrderNumComparator());
	}

	public static Map<String, Counter> sortMapByCount(
			Map<String, Counter> oriMap) {
		return sortMap(oriMap, new MapValueCounterCountComparator());
	}

	public static class MapValueCounterOrderNumComparator implements
			Comparator<Map.Entry<String, Counter>> {

		@Override
		public int compare(Map.Entry<String, Counter> o1,
				Map.Entry<String, Counter> o2) {

			return o1.getValue().getOrderNum()
					.compareTo(o2.getValue().getOrderNum());
		}
	}

	public static class MapValueCounterCountComparator implements
			Comparator<Map.Entry<String, Counter>> {

		@Override
		public int compare(Map.Entry<String, Counter> o1,
				Map.Entry<String, Counter> o2) {

			return o2.getValue().getCount().compareTo(o1.getValue().getCount());
		}
	}

	public static class MapValueLineCountComparator implements
			Comparator<Map.Entry<String, Line>> {

		@Override
		public int compare(Map.Entry<String, Line> o1,
				Map.Entry<String, Line> o2) {
			List<Object> l1 = o1.getValue().data();
			List<Object> l2 = o2.getValue().data();

			Float oo1 = Float.valueOf(l1.get(l1.size() - 1).toString());

			Float oo2 = Float.valueOf(l2.get(l2.size() - 1).toString());

			return oo2.compareTo(oo1);

		}
	}

}
